//
//  SetViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "SetViewController.h"
#import "BaseNavigationViewController.h"
#import "LoginViewController.h"
#import "PersonSetViewController.h"
#import "CodeViewController.h"
#import "SafeCenterViewController.h"
#import "ConnectPhoneViewController.h"
#import "FeedbackViewController.h"
#import "AboutViewController.h"
#import "SetPassWordController.h"

@interface SetViewController ()<UITableViewDataSource, UITableViewDelegate>{
    UIImageView *_headImageView;//头像
    NSString *name;
    NSString *phontNumber;
    NSString *backGround;
}

@property (nonatomic, strong) BaseTableView *tableView;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    name = _dict[@"userInfo"][@"nickName"];
    phontNumber = _dict[@"userInfo"][@"phone"];
    backGround = _dict[@"userInfo"][@"background"];
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    [self.view addSubview:self.tableView];
    
    UIButton *quitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), SCREEN_WIDTH, ScreenX375(49))];
    [quitBtn setTitle:@"退出" forState:UIControlStateNormal];
    [quitBtn setTitleColor:REXADECIMALCOLOR(@"#f21934") forState:UIControlStateNormal];
    quitBtn.titleLabel.font = Kfont(18);
    quitBtn.backgroundColor = [UIColor whiteColor];
    [quitBtn addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quitBtn];
}

- (void)quit {
    kNSUDefaultSaveVauleAndKey(@"", @"Authorization");
    [kUserDefaults synchronize];
    
    kKeyWindow.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else{
        return 6;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *cellId = @"InfoCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subView in cell.subviews) {
            [subView removeFromSuperview];
        }

        if (!_headImageView) {
            _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(286), ScreenX375(16), ScreenX375(56), ScreenX375(56))];
            _headImageView.clipsToBounds = YES;
            _headImageView.layer.cornerRadius = ScreenX375(28);
            if ([_dict[@"userInfo"][@"pic"] class] != [NSNull class]) {
                [_headImageView sd_setImageWithURL:[NSURL URLWithString:_dict[@"userInfo"][@"pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
            }
        }
        [cell addSubview:_headImageView];
        
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(37), ScreenX375(80), ScreenX375(15))];
        titleLable.font = Kfont(15);
        titleLable.textColor = REXADECIMALCOLOR(@"#333333");
        titleLable.text = @"个人信息";
        [cell addSubview:titleLable];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(90) - 0.5, SCREEN_WIDTH, 0.5)];
        line.backgroundColor = LineColor;
        [cell addSubview:line];

        return cell;
    }else {
        NSArray *noteArr = @[@"手机设置",@"密码设置",@"安全中心",@"建议反馈",@"关于我们",@"我的二维码"];
        static NSString *cellId = @"otherCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subView in cell.subviews) {
            [subView removeFromSuperview];
        }
        
        UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(17), ScreenX375(80), ScreenX375(15))];
        noteLabel.text = noteArr[indexPath.row];
        noteLabel.font = Kfont(15);
        noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
        [cell addSubview:noteLabel];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(48), ScreenX375(359), ScreenX375(0.5))];
        line.backgroundColor = LineColor;
        [cell addSubview:line];

        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return ScreenX375(90);
    }
    return ScreenX375(50);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return ScreenX375(80);
    }else{
        return ScreenX375(0);
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }else{
        UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(80))];
        baseView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
        return baseView;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section == 0) {
        PersonSetViewController *vc = [[PersonSetViewController alloc] init];
        vc.dict = _dict;
        vc.saveClick = ^(UIImage *image, NSString *name) {
            self->_headImageView.image = image;
            name = name;
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 0 && indexPath.section == 1) {
        ConnectPhoneViewController *vc = [[ConnectPhoneViewController alloc] init];
        vc.phone = phontNumber;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1 && indexPath.section == 1){
        SetPassWordController *vc = [[SetPassWordController alloc] init];
        vc.phone = phontNumber;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 2 && indexPath.section == 1) {
        SafeCenterViewController *vc = [[SafeCenterViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 3 && indexPath.section == 1) {
        FeedbackViewController *vc = [[FeedbackViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 4 && indexPath.section == 1) {
        AboutViewController *vc = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 5 && indexPath.section == 1) {
        CodeViewController *vc = [[CodeViewController alloc] init];
        vc.backGround = backGround;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
